package com.ruoyi.web.controller.system;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import com.ruoyi.system.domain.SysRural;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysMedical;
import com.ruoyi.system.service.ISysMedicalService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 医疗机构管理Controller
 *
 * @author moweiyu
 * @date 2021-04-30
 */
@Controller
@RequestMapping("/system/medical")
public class SysMedicalController extends BaseController
{
    private String prefix = "system/medical";

    @Autowired
    private ISysMedicalService sysMedicalService;

    @RequiresPermissions("system:medical:view")
    @GetMapping()
    public String medical()
    {
        return prefix + "/medical";
    }

    /**
     * 查询医疗机构管理列表
     */
    @RequiresPermissions("system:medical:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysMedical sysMedical)
    {
        startPage();
        List<SysMedical> list = sysMedicalService.selectSysMedicalList(sysMedical);
        return getDataTable(list);
    }

    /**
     * 通过 medicalName 查询行政区域 Ajax （新增页面）
     */
    @RequiresPermissions("system:medical:list")
    @PostMapping("/listAjax")
    @ResponseBody
    public void listAjax(HttpServletResponse response, SysMedical sysMedical) throws IOException {

        List<SysMedical> list = sysMedicalService.selectSysMedical(sysMedical);

        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

        String data = null;
        if (list.size() != 0) {
            data = "{\"res\":\"fail\"}";
        } else {
            data = "{\"res\":\"success\"}";
        }

        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json; charset=utf-8");
        response.getWriter().write(data);
    }

    /**
     * 导出医疗机构管理列表
     */
    @RequiresPermissions("system:medical:export")
    @Log(title = "医疗机构管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysMedical sysMedical)
    {
        List<SysMedical> list = sysMedicalService.selectSysMedicalList(sysMedical);
        ExcelUtil<SysMedical> util = new ExcelUtil<SysMedical>(SysMedical.class);
        return util.exportExcel(list, "医疗机构管理数据");
    }

    /**
     * 新增医疗机构管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存医疗机构管理
     */
    @RequiresPermissions("system:medical:add")
    @Log(title = "医疗机构管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SysMedical sysMedical)
    {
        return toAjax(sysMedicalService.insertSysMedical(sysMedical));
    }

    /**
     * 修改医疗机构管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        SysMedical sysMedical = sysMedicalService.selectSysMedicalById(id);
        mmap.put("sysMedical", sysMedical);
        return prefix + "/edit";
    }

    /**
     * 修改保存医疗机构管理
     */
    @RequiresPermissions("system:medical:edit")
    @Log(title = "医疗机构管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SysMedical sysMedical)
    {
        return toAjax(sysMedicalService.updateSysMedical(sysMedical));
    }

    /**
     * 删除医疗机构管理
     */
    @RequiresPermissions("system:medical:remove")
    @Log(title = "医疗机构管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(sysMedicalService.deleteSysMedicalByIds(ids));
    }
}
